package fnord.jovian.tango;

import fnord.jovian.TheBoard;

public class TheWipeTimer extends javax.swing.Timer {

    /**
     * 
     */
    private static final long serialVersionUID = -2233889707662770432L;
	private TheBoard _board;
    private static double ticks = 50.0;
    private static double[] a = new double[]{0,
    	0.00001,
    	0.00003,
    	0.00007,
    	0.00012,
    	0.000125,
    	0.00025,
    	0.0005,
    	0.001,
    	0.002,
    	0.009,
    	0.014,
    	0.02,
    	0.026,
    	0.031,
    	0.038,
    	0.039,
    	0.0395,
    	0.03975,
    	0.039875,
    	0.03988,
    	0.03993,
    	0.03997,
    	0.03999,
    	0.04,
    };
    private double _progress = 0;
    private Direction _dir;
    public enum Direction{LEFT,RIGHT,UP,DOWN};
    

    public TheWipeTimer(TheBoard board, Direction dir, double sec) {

        super((int)(sec*1000.0/ticks), null);

        _board = board;
        _dir = dir;
        
        this.setRepeats(true);

        this.addActionListener(new TheWipeListener());
    }
    
    private double accelerate() {
    	return a[(int)(_progress % ticks/2.0)];
    }

    private class TheWipeListener implements java.awt.event.ActionListener {

        public void actionPerformed(java.awt.event.ActionEvent e) {
        	_progress++;
        	switch(_dir) {
        	case LEFT:
            	_board.shift(-Math.PI/2.0*accelerate());
            	break;
        	case RIGHT:
            	_board.shift(Math.PI/2.0*accelerate());
            	break;
        	case UP:
            	_board.shift(0,-_board.getHeight()*accelerate());
            	break;
        	case DOWN:
            	_board.shift(0,_board.getHeight()*accelerate());
            	break;
        	}
            if (_progress >= ticks) {
            	_board.finishShift();
            	_progress = 0;
            	setRepeats(false);
            	stop();
            }
            _board.repaint();
        }
    }
}